$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'employeeRole/list',
        datatype: "json",
        colModel: [
            { label: '角色id', hidden:true, name: 'roleId', index: "role_id", width: 45, key: true },
            { label: '经营主体名称', name: 'subjectName', index: "subject_name", width: 100},
            { label: '角色名称', name: 'roleName', index: "role_name", width: 60},
            { label: '创建时间', name: 'createTime', index: "create_time", width: 80},
            { label: '备注', name: 'remark', width: 100 }
        ],
        viewrecords: true,
        height: (pageInfo().height-130),
        rowNum: 15,
        rowList : [15,50,99],
        loadtext : '<img src="../../statics/plugins/layer/skin/default/loading-1.gif" style="margin-right:10px;"/>正在加载,请稍候……',
        rownumbers: false,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        loadComplete : function(data){
            if(data.code == 0){
                return data;
            }else if(data.code == 205){
                invalidAccess();
            }else{
                layer.alert(data.msg);
            }
        },
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

//菜单树
var menu_ztree;
var menu_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: 0
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:true,
        nocheckInherit:true
    }
};

var vm = new Vue({
    el:'#witapp',
    data:{
        q:{
            roleName : null
        },
        showList : true,
        title : null,
        role : {
            deptId : null,
            deptName : null
        },
        flagAdd : true,
        roleId : null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.flagAdd = true;
            vm.roleId = null;
            vm.showList = false;
            vm.role.menuIdList = [];
            vm.title = "新增";
            vm.role = {deptName:null, deptId:null};
            vm.getMenuTree(null);
        },
        update: function () {
            var roleId = getSelectedRow();
            if(roleId){
                vm.flagAdd = false;
                vm.roleId = roleId;
                vm.showList = false;
                vm.title = "修改";
                vm.getMenuTree(roleId);
            }
        },
        del: function () {
            var roleIds = getSelectedRows();
            if(roleIds == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                ajaxExecute(baseURL + "employeeRole/delete",JSON.stringify(roleIds),function(data){
                    if(data.code == 0){
                        vm.reload();
                    }else{
                        alert(data.msg);
                    }
                });
            });
        },
        /*勾选'员工角色'对应的菜单数据*/
        getRole: function(roleId){
            var params = {};
            if(roleId != null && roleId.length >0){
                params = {roleId:roleId}
            }
            ajaxQuery(baseURL + "employeeRole/info/",params,function(data){
                if(data.code == 0){
                    vm.role = data.role;
                    //勾选角色所拥有的菜单
                    var menuIds = vm.role.menuIdList;
                    for(var i=0; i<menuIds.length; i++) {
                        var node = menu_ztree.getNodeByParam("menuId", menuIds[i]);
                        menu_ztree.checkNode(node,true,false);
                    }
                }
            });
        },
        saveOrUpdate: function (){
            var roleName = vm.role.roleName;
            if(roleName == null || roleName.length <= 0){
                alert('请输入员工角色名称');
                return;
            }
            //获取选择的菜单
            var nodes = menu_ztree.getCheckedNodes(true);
            var menuIdList = new Array();
            for(var i=0; i<nodes.length; i++) {
                menuIdList.push(nodes[i].menuId);
            }
            vm.role.deptId = 1;
            vm.role.menuIdList = menuIdList;
            var url = "employeeRole/update";
            vm.role.roleId = vm.roleId;
            if(vm.flagAdd){
                url = "employeeRole/save";
                vm.role.roleId = null;
            }
            ajaxExecute(baseURL + url,JSON.stringify(vm.role),function(data){
                if(data.code === 0){
                    dialogResult(data.msg);
                    vm.reload();
                }else{
                    alert(data.msg);
                }
            });
        },
        //加载当前账号所拥有的权限菜单
        getMenuTree: function(roleId){
            ajaxQuery(baseURL + "employeeRole/select",{},function(data){
                if(data.code == 0){
                    menu_ztree = $.fn.zTree.init($("#menuTree"), menu_setting,data.data);
                    //展开所有节点
                    menu_ztree.expandAll(true);
                    vm.getRole(roleId);
                }else{
                    $('#menuTree').html('<li style="color:#f83a6f;font-size:14px;">'+data.msg+'</li>');
                }
            });
        },
        reload: function (){
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'roleName': vm.q.roleName},
                page:page
            }).trigger("reloadGrid");
        }
    }
});